package com.JZ.QM.controller;

import com.JZ.QM.common.entity.PositionDetailEntity;
import com.JZ.QM.common.entity.UserPositionDetailEntity;
import com.JZ.QM.common.utils.CommonResult;
import com.JZ.QM.service.WealthPositionService;
import javafx.geometry.Pos;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Iterator;

/**
 * @author: cyc
 * @email cyc@gmail.com
 * @date: 2020/7/12 22:09
 */
@RestController
@RefreshScope
@Slf4j
@RequestMapping(value="/wealthPosition")
public class WealthPositionController {

    @Resource
    WealthPositionService wealthPositionService;

    /**
     * 展示持仓列表
     * @param accountId 财富账号的id
     * @return
     */
    @RequestMapping("/showPositionDetailItems")
    public CommonResult showPositionDetailItems(Long accountId){
        ArrayList<PositionDetailEntity> positionDetailEntities=new ArrayList();
        PositionDetailEntity positionDetailEntity=new PositionDetailEntity();
        BigDecimal totalMoney=new BigDecimal(0);

        //通过财富账号来获取对应的持仓明细详细信息列表
        positionDetailEntities=wealthPositionService.getWealthPositionProducts(accountId);
        if(positionDetailEntities.isEmpty()){
            return CommonResult.success().put("positionDetailItems",new ArrayList().toArray()).put("totalMoney",new BigDecimal(0));
        } else {
            //更新持仓信息表中的组合的总收益
            for (Iterator<PositionDetailEntity> it = positionDetailEntities.iterator(); it.hasNext(); ) {
                positionDetailEntity= it.next();
                if(positionDetailEntity.getTotalMoney() !=null){
                    totalMoney=totalMoney.add(positionDetailEntity.getTotalMoney());
                }
            }
            return CommonResult.success().put("positionDetailItems",positionDetailEntities).put("totalMoney",totalMoney);
        }
    }

    /**
     * 展示所有用户持仓的前五名
     * @param comBaseId 原组合id
     * @return
     */
    @RequestMapping("/showPositionTop5ForUser")
    public CommonResult showPositionTop5ForUser(Long comBaseId){
        //持仓详细信息记录
        ArrayList<UserPositionDetailEntity> positionDetailEntities=new ArrayList<>();

        //通过组合id，获取所有购买该组合的金额最多的无名用户
        positionDetailEntities=wealthPositionService.showPositionTop5ForUser(comBaseId);
        if(positionDetailEntities.isEmpty()){
            return CommonResult.success().put("userPositionList",new ArrayList().toArray());
        } else {
            return CommonResult.success().put("userPositionList",positionDetailEntities);
        }
    }


}
